<template>
  <div class="list-table" style="width: 98%; margin: auto; height: 96%; margin-top: 1%;">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('标题')" prop="shareTitle">
        <el-input v-model="queryParams.shareTitle" :placeholder="$t('标题')" />
      </el-form-item>
      <el-form-item :label="$t('能否编辑')" prop="edit">
        <el-select v-model="queryParams.edit" :placeholder="$t('能否编辑')">
          <el-option
            v-for="(item, index) in editOptions"
            :key="index"
            :label="$t(item.dictLabel)"
            :value="item.dictValue"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('能否下载')" prop="download">
        <el-select v-model="queryParams.download" :placeholder="$t('能否编辑')">
          <el-option
            v-for="(item, index) in downloadOptions"
            :key="index"
            :label="$t(item.dictLabel)"
            :value="item.dictValue"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sysShareFilesList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('标题')" align="center" prop="shareTitle" show-overflow-tooltip min-width="250">
        <template slot-scope="scope">
          <el-link :underline="false" type="primary" @click="updateHandle(scope.row)">{{ $t(scope.row.shareTitle) }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('能否编辑')" align="center" prop="edit" show-overflow-tooltip :formatter="editFormat" min-width="100" />
      <el-table-column :label="$t('能否下载')" align="center" prop="download" show-overflow-tooltip :formatter="downloadFormat" min-width="100" />
      <el-table-column :label="$t('分享状态')" align="center" show-overflow-tooltip prop="enabledState">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabledState"
            active-value="0"
            inactive-value="1"
            @change="enabledStateSwitch(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="220">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('编辑') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-view"
            @click="recordingHandle(scope.row)"
          >{{ $t('记录') }}</el-button>
          <span style="margin-left: 10px; margin-right: 10px;">
            <el-tooltip class="item" effect="dark" :content="scope.row.url" placement="top-start">
              <router-link
                target="_blank"
                :to="{ path:'/SysShareHome?id=' + scope.row.id, query:{ id: scope.row.id } }"
                class="link_a el-icon-link"
              >{{ $t('跳转') }}</router-link>
            </el-tooltip>
          </span>
          <el-button
            v-clipboard:copy="scope.row.shareTitle + '\n' + $t('过期时间') + ': ' + judgmentTime(scope.row) + '\n' + $t('分享链接') + ': ' + scope.row.url"
            v-clipboard:success="onCopy"
            v-clipboard:error="onError"
            :disabled="scope.row.enabledState !== '0'"
            size="small"
            type="text"
            icon="el-icon-document-copy"
          >{{ $t('分享') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 访问记录 -->
    <el-dialog :title="$t(title)" :visible.sync="sysSharingAccessRecordOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <sysSharingAccessRecordList ref="sysSharingAccessRecordList" />
    </el-dialog>

    <!-- 添加或修改文件分享对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="shareFilesFormOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <sysShareFilesForm ref="sysShareFilesForm" :from-record-page-file-id="fromRecordPageFileId" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listSysShareFiles,
  delSysShareFiles,
  enabledStateSwitch,
  findListRecord
} from '@/api/file/share/sysShareFiles.js'
import sysSharingAccessRecordList from '@/views/file/share/sysSharingAccessRecordList.vue'
export default {
  components: {
    sysShareFilesForm: () => import('./sysShareFilesForm.vue'),
    sysSharingAccessRecordList
  },
  props: {
    // 如果存在ID, 就是来自操作日志页面, 否则走正常加载
    fromRecordPageFileId: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 文件分享表格数据
      sysShareFilesList: [],
      // 弹出层标题
      title: '文件分享',
      // 是否显示弹出层
      shareFilesFormOpen: false,
      sysSharingAccessRecordOpen: false,
      // 查看人能否编辑字典
      editOptions: [],
      // 查看人能否下载字典
      downloadOptions: [],
      // 查询参数
      queryParams: {
        shareTitle: undefined,
        edit: undefined,
        download: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      copyText: ''
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_yes_no').then(response => {
      console.log(response)
      this.editOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.downloadOptions = response.data
    })
  },
  methods: {
    /** 查询文件分享列表 */
    getList() {
      this.loading = true
      if (this.fromRecordPageFileId !== undefined && this.fromRecordPageFileId !== null && this.fromRecordPageFileId !== '') {
        var tempQuery = JSON.parse(JSON.stringify(this.queryParams))
        tempQuery.fileId = this.fromRecordPageFileId
        findListRecord(tempQuery).then(response => {
          this.sysShareFilesList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        listSysShareFiles(this.queryParams).then(response => {
          this.sysShareFilesList = response.rows
          this.total = response.total
          this.loading = false
        })
      }
    },
    judgmentTime(row) {
      const resultTime = row.aging === null ? this.$t('长期有效') : this.parseTime(row.aging, '{y}-{m}-{d} {h}:{i}:{s}')
      console.log(resultTime)
      return resultTime
    },
    onCopy() {
      this.msgSuccess(this.$t('复制成功!'))
    },
    onError() {
      this.msgError(this.$t('复制失败!'))
    },
    // 查看人能否编辑字典翻译
    editFormat(row) {
      return this.selectDictLabel(this.editOptions, row.edit)
    },
    // 查看人能否下载字典翻译
    downloadFormat(row) {
      return this.selectDictLabel(this.downloadOptions, row.download)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        url: undefined,
        edit: undefined,
        download: undefined,
        userId: undefined,
        createBy: undefined,
        createTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.shareFilesFormOpen = true
      this.$nextTick(() => {
        this.$refs.sysShareFilesForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.shareFilesFormOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.sysShareFilesForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delSysShareFiles(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /* 分享状态 */
    enabledStateSwitch(row) {
      const text = row.enabledState === '0' ? this.$t('开启分享') : this.$t('关闭分享')
      this.$confirm(this.$t('确认要') + text + '?', this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return enabledStateSwitch(row.id, row.enabledState)
      }).then(() => {
        this.msgSuccess(text + this.$t('成功'))
      }).catch(function() {
        row.enabledState = row.enabledState === '0' ? '1' : '0'
      })
    },
    /** 访问记录 */
    recordingHandle(row) {
      this.title = '访问记录'
      this.sysSharingAccessRecordOpen = true
      this.$nextTick(() => {
        this.$refs.sysSharingAccessRecordList.shareFileId = row.id
        this.$refs.sysSharingAccessRecordList.getList()
      })
    }
  }
}
</script>
